<?php
require_once("include/header.php");


$username = $_POST['username'];
$password = $_POST['password'];
$nationality = $_POST['nationality'];
$sex = $_POST['sex'];

try {
	$retVal = insert_new_userLogin_to_db($username, $password, 0, 1);
	
	if ($retVal == false) {
		$conVal = connectToDB();
		if ($conVal == false)
			throw new Exception("Err1");
		else {
			$retVal = insert_new_userLogin_to_db($username, $password, 0, 1);
			if ($retVal == false)
				throw new Exception("Err2");
		}
	}	
	$retVal = insert_new_userData_to_db($username, "", "", $sex, 0, $nationality);  
	
	if ($retVal == false) {
		$conVal = connectToDB();
		if ($conVal == false)
			throw new Exception("Err1");
		else {
			$retVal = insert_new_userData_to_db($username, "", "", $sex, 0, $nationality); 
			if ($retVal == false)
				throw new Exception("Err3");
		}
	}
	
	header("Location:createAccount_group.php");	
}
catch (Exception $er) {
	if ($er == "Err1")
		echo "<h1 style=\"color:red\">Cannot connect to database, please try later. We are sorry for the inconvenience</h1>";
	if ($er == "Err2")
		echo "<h1 style=\"color:red\">Cannot create new account, please try later. We are sorry for the inconvenience</h1>";
	if ($er == "Err3") {
		echo "<h1 style=\"color:red\">Cannot create new account, please try later. We are sorry for the inconvenience</h1>";
		/* TODO: DELETE NEWLY CREATED LOGIN ACCOUNT */
	}
}


/*
mysql_db_query(DB_NAME,"INSERT INTO ".DB_TABLE_LOGIN." (user,pass,active,rights) 
	    VALUES ('".$_POST['user']."', PASSWORD('".$_POST['pass']."'), '".$_POST['banned']."' , '".$_POST['rights']."')");

mysql_db_query(DB_NAME,"INSERT INTO ".DB_TABLE_USER_INFO." (user,name,surname,sex,country,
	    points,girl_count,group_id,likes_animals,description, rights, age) 
	    VALUES ('".$_POST['user']."', '".$_POST['name']."', '"
	    .$_POST['surname']."' , '".$_POST['sex']."' , '".$_POST['country']."'
	    , '".$_POST['points']."', '".$_POST['girl_count']."', '".$_POST['group_id']."'
	    , '".$_POST['likes_animals']."', '".$_POST['description']."', '".$_POST['rights']."', '".$_POST['age']."')");

    $user=$_POST['user'];
    $pass=$_POST['pass'];
  
    $db_res = mysql_query("SELECT * FROM ".DB_TABLE_LOGIN." WHERE user='".mysql_real_escape_string($user)."' AND pass=PASSWORD('".mysql_real_escape_string($pass)."')");
    $user_array=mysql_fetch_array($db_res);
    $_SESSION["session_user"]= $user_array['user'];
    $_SESSION["session_pass"]= $user_array['pass'];
    $_SESSION["session_active"]= $user_array['active'];
    $_SESSION["session_banned"]= $user_array['banned'];
    $_SESSION["session_rights"]= $user_array['rights'];

*/
?>